<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="pr-1 m-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handlePrint({})" v-auth="'mo:rd10:wmsRd10Apply:view'">
          <Icon icon="simple-line-icons:printer" /> {{ t('入库单打印') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
    <PrintModal @register="registerPrintModal" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoRd10WmsRd10ApplyList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { wmsRd10ApplyDelete, wmsRd10ApplyListData } from '/@/api/wms/mo/rd10/wmsRd10Apply';
  import { useModal } from '/@/components/Modal';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import PrintModal from '/@/components/print/printModal.vue';

  const { t } = useI18n('mo.rd10.wmsRd10Apply');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('产品入库申请管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    fieldMapToTime: [
      ['createDate', ['createDate_gte', 'createDate_lte']],
      ['ddate', ['ddate_gte', 'ddate_lte']],
    ],
    schemas: [
      {
        label: t('申请单号'),
        field: 'code',
        component: 'Input',
      },
      {
        label: t('订单号'),
        field: 'mocode',
        component: 'Input',
      },
      {
        label: t('行号'),
        field: 'sortseq',
        component: 'Input',
      },
      {
        label: t('存货编码'),
        field: 'invcode',
        component: 'Input',
      },
      {
        label: t('存货名称'),
        field: 'basInv.cinvname',
        component: 'Input',
      },
      {
        label: t('仓库'),
        field: 'basWare.cwhname',
        component: 'Input',
      },
      {
        label: t('生产批次'),
        field: 'cbatch',
        component: 'Input',
      },
      {
        label: t('部门'),
        field: 'mdeptname',
        fieldLabel: 'mdeptname',
        component: 'TreeSelect',
        componentProps: {
          dictType: 'dept',
          allowClear: true,
          canSelectParent: false,
        },
      },
      {
        label: t('申请人'),
        field: 'creator.userName',
        component: 'Input',
      },
      {
        label: t('申请日期'),
        field: 'createDate',
        component: 'RangePicker',
      },
      {
        label: t('订单日期'),
        field: 'ddate',
        component: 'RangePicker',
      },
      {
        label: t('状态'),
        field: 'status_in',
        component: 'Select',
        componentProps: {
          dictType: 'bpm_biz_status',
          mode: 'multiple', // 多选
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('状态'),
      dataIndex: 'status',
      key: 'a.status',
      sorter: true,
      width: 80,
      align: 'center',
      dictType: 'bpm_biz_status ',
    },
    {
      title: t('申请单号'),
      dataIndex: 'code',
      key: 'a.code',
      sorter: true,
      width: 130,
      align: 'left',
      slot: 'firstColumn',
    },
    {
      title: t('申请日期'),
      dataIndex: 'createDate',
      key: 'a.create_date',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('订单日期'),
      dataIndex: 'ddate',
      key: 'a.ddate',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('订单号'),
      dataIndex: 'mocode',
      key: 'a.mocode',
      sorter: true,
      width: 140,
      align: 'left',
    },
    {
      title: t('行号'),
      dataIndex: 'sortseq',
      key: 'a.sortseq',
      sorter: true,
      width: 60,
      align: 'center',
    },
    {
      title: t('存货编码'),
      dataIndex: 'invcode',
      key: 'a.invcode',
      sorter: true,
      width: 120,
      align: 'left',
    },
    {
      title: t('存货名称'),
      dataIndex: 'basInv.cinvname',
      key: 'inv.cinvname',
      sorter: true,
      width: 300,
      align: 'left',
    },
    {
      title: t('规格型号'),
      dataIndex: 'cinvstd',
      key: 'inv.cinvstd',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('部门'),
      dataIndex: 'mdeptname',
      key: 'a.mdeptname',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('仓库'),
      dataIndex: 'basWare.cwhname',
      key: 'a.cwhcode',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('生产批次'),
      dataIndex: 'cbatch',
      key: 'a.cbatch',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('数量'),
      dataIndex: 'iqty',
      key: 'a.iqty',
      sorter: true,
      width: 80,
      align: 'right',
    },
    {
      title: t('申请人'),
      dataIndex: 'creator.userName',
      key: 'a.create_by',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('U8单据号'),
      dataIndex: 'u8Code',
      key: 'a.u8_code',
      sorter: true,
      width: 135,
      align: 'left',
    },
    {
      title: t('子表ID'),
      dataIndex: 'modid',
      key: 'a.modid',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('流程节点'),
      dataIndex: 'lcjd',
      key: 'lcjd',
      width: 120,
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 130,
    actions: (record: Recordable) => [
      {
        icon: 'clarity:note-edit-line',
        title: t('编辑产品入库申请'),
        onClick: handleForm.bind(this, { code: record.code }),
        ifShow: record.status !== '0' && record.status !== '1',
        auth: 'mo:rd10:wmsRd10Apply:edit',
      },
      {
        icon: 'ant-design:delete-outlined',
        color: 'error',
        title: t('删除产品入库申请'),
        popConfirm: {
          title: t('是否确认删除产品入库申请'),
          confirm: handleDelete.bind(this, { code: record.code }),
        },
        ifShow: record.status === '9',
        auth: 'mo:rd10:wmsRd10Apply:edit',
      },
      {
        icon: 'fluent:flowchart-20-regular',
        title: t('流程追踪'),
        onClick: handleTrace.bind(this, record),
        ifShow: () => record.status != '9',
      },
    ],
  };

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows }] = useTable({
    api: wmsRd10ApplyListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await wmsRd10ApplyDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  function handleSuccess() {
    reload();
  }

  const [registerPrintModal, { openModal: openPrintModal }] = useModal();
  const [registerTraceModal, { openModal: traceModel }] = useModal();
  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'rd10_apply', bizKey: record.id });
  }

  // 入库单打印
  async function handlePrint() {
    let arr = await getSelectRows();
    if (getSelectRows().length == 0) {
      showMessage(t('请先选择一行数据'));
      return;
    }
    const idsArr = arr.map((item) => {
      return item.id;
    });
    let params = {
      ids: idsArr.join(','),
      title: '产成品入库单打印',
      height: '0.9',
      width: '0.9',
      fileName: '产成品入库单',
    };
    openPrintModal(true, params);
  }

  onMounted(async () => {
    handleSearch();
  });

  async function handleSearch() {
    await getForm().setFieldsValue({
      status_in: '4',
    });
    reload();
    await getForm().setFieldsValue({
      status_in: '4',
    });
  }
</script>
